package com.ruoyi.recycleshop.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.recycle.domain.ShopManagement;
import com.ruoyi.recycle.dto.LoginDTO;
import com.ruoyi.recycle.service.IShopManagementService;
import com.ruoyi.recycle.vo.LoginVO;
import com.ruoyi.recycleshop.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/login")
@Api(tags = "登录")
public class LoginController extends BaseController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private IShopManagementService shopManagementService;


    @PostMapping
    @ApiOperation("登录接口")
    public R<?> login(@RequestBody LoginDTO loginDTO) {
        try {
            final LoginVO vo = loginService.login(loginDTO);
            return R.ok(vo);
        } catch (Exception e) {
            return R.fail("登陆失败", e.getMessage());
        }

    }

    @ApiOperation("微信授权获取用户openId")
    @GetMapping("/wechatAuth")
    public R<String> wechatAuth(@RequestParam String encryptedData, @RequestParam String iv, @RequestParam String code) {
        return R.ok(loginService.wechatAuth(code, encryptedData, iv));
    }


    @GetMapping("/refreshShopInfo")
    @ApiOperation("获取门店信息")
    @ApiImplicitParam(name = "shopId", value = "门店ID")
    public R<ShopManagement> refreshShopInfo(@RequestParam Long shopId) {
        return R.ok(shopManagementService.getById(shopId));
    }

    @GetMapping("/logout")
    @ApiOperation("退出登录")
    public R logout(HttpServletRequest request) {
        return loginService.logout(request);
    }
}
